package cn.csc.module.pay.dal.mysql.jzbsuborder;

import cn.csc.framework.mybatis.core.mapper.BaseMapperX;
import cn.csc.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csc.framework.mybatis.core.query.LambdaUpdateWrapperX;
import cn.csc.module.pay.dal.dataobject.jzbsuborder.PayJzbSubOrderDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.util.StringUtils;

import java.math.BigInteger;
import java.util.List;
import java.util.Optional;

/**
 * 见证宝子订单信息 Mapper
 *
 * @author liuchuang
 */
@Mapper
public interface PayJzbSubOrderMapper extends BaseMapperX<PayJzbSubOrderDO> {

    default PayJzbSubOrderDO selectByJzbOrderId( BigInteger merchantId, BigInteger jzbOrderId ) {
        return selectOne(new LambdaQueryWrapperX<PayJzbSubOrderDO>()
                .eq(PayJzbSubOrderDO::getJzbOrderId, jzbOrderId)
                .eq(PayJzbSubOrderDO::getMerchantId, merchantId)
                );
    }

    default List<PayJzbSubOrderDO> selectListByJzbOrderId( BigInteger merchantId, BigInteger jzbOrderId ) {
        return selectList(new LambdaQueryWrapperX<PayJzbSubOrderDO>()
                .eq(PayJzbSubOrderDO::getJzbOrderId, jzbOrderId)
                .eqIfPresent(PayJzbSubOrderDO::getMerchantId, merchantId)
        );
    }

    default List<PayJzbSubOrderDO> selectListByOrderStatus( Integer orderStatus ) {
        return selectList(new LambdaQueryWrapperX<PayJzbSubOrderDO>()
                .eq(PayJzbSubOrderDO::getOrderStatus, orderStatus)
        );
    }

    default PayJzbSubOrderDO selectByOrderItemId(BigInteger merchantId, BigInteger jzbOrderId , String appUserId, BigInteger orderItemId) {
        return selectOne(new LambdaQueryWrapperX<PayJzbSubOrderDO>()
                .eq(PayJzbSubOrderDO::getJzbOrderId, jzbOrderId)
                .eq(PayJzbSubOrderDO::getMerchantId, merchantId)
                .eqIfPresent(PayJzbSubOrderDO::getAppUserId, appUserId)
                .eqIfPresent(PayJzbSubOrderDO::getOrderItemId, orderItemId)
        );
    }

    default PayJzbSubOrderDO selectByAppUserId(BigInteger merchantId, BigInteger jzbOrderId , String appUserId) {
        return selectByOrderItemId(merchantId, jzbOrderId, appUserId, null);
    }

    /**
     * 更新订单状态
     * 内部订单orderItemId必传
     * @param orderDO 子订单信息
     * @return
     */
    default Integer updateStatusByJzbOrderId(PayJzbSubOrderDO orderDO){
        LambdaUpdateWrapperX<PayJzbSubOrderDO> userUpdateWrapper = new LambdaUpdateWrapperX<>();
        userUpdateWrapper.set(PayJzbSubOrderDO::getOrderStatus, orderDO.getOrderStatus());
        userUpdateWrapper.set(StringUtils.hasLength(orderDO.getRechargeSeqNo()),PayJzbSubOrderDO::getRechargeSeqNo, orderDO.getRechargeSeqNo());
        userUpdateWrapper.set(StringUtils.hasLength(orderDO.getRechargeOrders()),PayJzbSubOrderDO::getRechargeOrders, orderDO.getRechargeOrders());
        userUpdateWrapper.set(StringUtils.hasLength(orderDO.getRechargeCancelSeqNo()),PayJzbSubOrderDO::getRechargeCancelSeqNo, orderDO.getRechargeCancelSeqNo());
        userUpdateWrapper.set(StringUtils.hasLength(orderDO.getRechargeCancelOrders()),PayJzbSubOrderDO::getRechargeCancelOrders, orderDO.getRechargeCancelOrders());
        userUpdateWrapper.set(StringUtils.hasLength(orderDO.getSubAcctSeqNo()),PayJzbSubOrderDO::getSubAcctSeqNo, orderDO.getSubAcctSeqNo());
        userUpdateWrapper.set(StringUtils.hasLength(orderDO.getSubAcctOrders()),PayJzbSubOrderDO::getSubAcctOrders, orderDO.getSubAcctOrders());
        userUpdateWrapper.set(StringUtils.hasLength(orderDO.getSubAcctCancelSeqNo()),PayJzbSubOrderDO::getSubAcctCancelSeqNo, orderDO.getSubAcctCancelSeqNo());
        userUpdateWrapper.set(StringUtils.hasLength(orderDO.getSubAcctCancelOrders()),PayJzbSubOrderDO::getSubAcctCancelOrders, orderDO.getSubAcctCancelOrders());

        userUpdateWrapper.eq(PayJzbSubOrderDO::getJzbOrderId, orderDO.getJzbOrderId());
        userUpdateWrapper.eq(orderDO.getOrderType()== 7,PayJzbSubOrderDO::getOrderItemId, orderDO.getOrderItemId());
        return update(null, userUpdateWrapper);
    }

    /**
     * 更新订单状态
     * @return
     */
    default Integer updateStatusByJzbOrderIdList(List<BigInteger> jzbOrderIds, Integer orderStatus){
        LambdaUpdateWrapper<PayJzbSubOrderDO> userUpdateWrapper = new LambdaUpdateWrapper<>();
        userUpdateWrapper.set(PayJzbSubOrderDO::getOrderStatus, orderStatus);
        userUpdateWrapper.in(PayJzbSubOrderDO::getJzbOrderId, jzbOrderIds);
        return update(null, userUpdateWrapper);
    }

    /**
     * 更新订单状态
     * 外部订单orderItemId为null
     * @return
     */
    default Integer updateStatusByJzbOrderIdAndOrderItemId(BigInteger jzbOrderId, BigInteger orderItemId, Integer orderStatus){
        LambdaUpdateWrapperX<PayJzbSubOrderDO> userUpdateWrapper = new LambdaUpdateWrapperX<>();
        userUpdateWrapper.set(PayJzbSubOrderDO::getOrderStatus, orderStatus);
        userUpdateWrapper.eq(PayJzbSubOrderDO::getJzbOrderId, jzbOrderId);
        userUpdateWrapper.eqIfPresent(PayJzbSubOrderDO::getOrderItemId, orderItemId);
        return update(null, userUpdateWrapper);
    }

}
